!
!    MUNILIST   Listing magnitude and time from FITS, MAT files.
!    Copyright (C) 1997-9  Filip Hroch, Masaryk University, Brno, CZ
!
!
!CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
!C                                                                      C
!C !!!!!!!!!!!!!!!!     This is UNIX version  !!!!!!!!!!!!!!!!!!!!!!!!! C
!C                                                                      C
!CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
!
! This source file contains UNIX specific functions:
!    getarg, iargc
!
!--------------------------------------------------------------------------

program main_for_munilist

use mlist

implicit none

! interface
!      subroutine munilist(namfits,column,nstars,stars,outlst, &
!                    min0,per,alpha,delta,sufix,status)
!
!      character(len=*) ::  namfits, sufix
!      logical :: outlst
!      integer :: column,nstars,stars(nstars),status
!       real(selected_real_kind(16)) :: min0,per,alpha,delta
!      end subroutine munilist
! end interface

integer, parameter :: LEN = 80
integer, parameter :: MAXSTARS = 500000
integer, parameter :: ICOLUMN = 4

integer :: i,j,l,k, icit = 1, quiet = 0, column=ICOLUMN
logical :: stdinput = .false. 
character(len=LEN) :: par, namfits=" ", namdir=" ", sufix=" ", outfile= " "
character(len=1) :: outlst = ' '
integer :: nstars = 0, stars(MAXSTARS,2)
real(selected_real_kind(14)) :: min0, per = -1.0, alpha = -1.0, delta, &
     tol = 10.0, long = -999.0, lat = -99.9
real :: x,y

!integer,external :: iargc

do i = 1, iargc()

   call getarg(i,par)

!  dir file
   if( par(1:1) == "@" ) then
      namdir = par(2:)
      stdinput = namdir == " "
   endif

! output file
   if( par == "-o" )then
     call getarg(i+1,outfile)
   endif

!  column
   if( par(1:4) == "col=" ) then
      read(par(5:),*,iostat=j) column
      if( j /= 0 ) then
        write(*,*) "Bad column format. Using defaults: column=",ICOLUMN
        column = ICOLUMN 
      endif
   endif

!  suffix
   if( par(1:4) == "suf=" ) &
     sufix = par(5:)

!  star - row
   read(par,*,iostat=j) x
   if( j == 0 .and. nstars < MAXSTARS ) then
      nstars = nstars + 1
      read(par,*,iostat=j) x,y
      if( j == 0 ) then
         l = nint(x)
         k = nint(y)         
         stars(nstars,1) = l
         stars(nstars,2) = k
      else
         l = nint(x)
         stars(nstars,1) = l
         stars(nstars,2) = 0
      endif
      if( nstars == MAXSTARS ) &
        write(*,*) "Next star(s) will be ingnored, a buffer for stars full." 
   endif

!  full output
   if( par == "-of" ) outlst = 'f'

!  intensities on output
   if( par == "-oi" ) outlst = 'i'

!  quiet
   if( par == "-q" ) quiet = 1

!  photometric elements 
   if( par(1:4) == "ele=" )then
      read(par(5:),*,iostat=j) min0,per
      if( j /= 0 )then
        write(*,*) "Bad element format. Parameters ignored."
        per = -1.0
      endif
   endif

!  spherical coordinates
   if( par(1:4) == "coo=" )then
      read(par(5:),*,iostat=j) alpha,delta
      if( j /= 0 )then
        write(*,*) "Bad coordinates format. Parameters ignored."
        alpha = -1.0
      endif
   endif

!  geographic coordinates
   if( par(1:4) == "geo=" )then
      read(par(5:),*,iostat=j) long,lat
      if( j /= 0 )then
        write(*,*) "Bad coordinates format. Parameters ignored."
        long = -1.0
      endif
   endif

!  radius for search
   if( par(1:4) == "tol=" )then
      read(par(5:),*,iostat=j) tol
      if( j /= 0 )then
        write(*,*) "Bad tolerance format. Parameters ignored."
        tol = 1.0
      endif
   endif
      
enddo

if( nstars == 0 .and. iargc() > 1 ) stop "No input stars."

! image name from file
if( namdir /= " " ) then
  open(11,file=namdir,status='old',iostat=j)
  if( j /= 0 ) then
     write(*,*) "Can't open file: ",namdir
     stop
  endif
  do 
     icit = icit + 1
     read(11,"(A)",end=20) namfits
     call munilist(namfits,column,nstars,stars,outlst, &
                   min0,per,alpha,delta,long,lat,sufix,j,quiet,tol)
     if( j /= 0 .and. quiet == 0 ) &
         write(*,*) "Can't read this data file: ",namfits
  enddo  
20 close(11)
endif

! image name by command line
do i = 1, iargc()
   call getarg(i,par)
   read(par,*,iostat=j) l
   if( index(par,"=") == 0 .and. par(1:1) /= "@" .and.  &
       par(1:1) /= "-" .and. j /= 0 ) then
     namfits = par
     call munilist(namfits,column,nstars,stars,outlst, &
                   min0,per,alpha,delta,long,lat,sufix,j,quiet,tol)
     if( j /= 0 .and. quiet == 0 ) &
          write(*,*) "Can't read this data file: ",namfits
     icit = icit + 1
   endif
enddo

! images name by standart input
if( stdinput ) then
  do
     icit = icit + 1
     read(*,"(A)",end=30) namfits
     call munilist(namfits,column,nstars,stars,outlst, &
                   min0,per,alpha,delta,long,lat,sufix,j,quiet,tol)
     if( j /= 0 .and. quiet == 0 ) &
        write(*,*) "Can't read this data file: ",namfits
  enddo  
30 continue
endif

if( iargc() < 1 .and. icit == 1) then
  write(*,*) "MUNILIST Version 1.3, Copyright (C) 1997-6 F.Hroch, Masaryk University, Brno,CZ"
  write(*,*) "Munilist comes with ABSOLUTELY NO WARRANTY; This is free software, and you are"
  write(*,*) "welcome to redistribute under certain conditions; see 'gnulic.doc' for details."
  write(*,*)
  write(*,*) "MUNILIST   Listing magnitude and time from FITS, MAT files."
  write(*,*)
  write(*,*) "Usage:munilist [@dirfile] [col=column] [-of] [line_var] [line_comp1] .." 
  write(*,*) "               [ele=min0,period] [suf=suffix] [image1].."
  write(*,*)
  write(*,*) "@dirfile= file with name(s) of stars catalogue(s)"
  write(*,*) "          (for only '@', read catalogue(s) name(s) from standart input)"
!  write(*,*) "-o file= name of output file"
  write(*,*) "column= column for listing (4 = Aperture 1, default)"
  write(*,*) "-of= full output list (default short output list)"
  write(*,*) "-oi= like -of, but intensities (no magnitudes) on output"
  write(*,*) "     (with coo=, geo= include azimutal coordinates and airmass in list)"
  write(*,*) "-q = quiet"
  write(*,*) "coo= alpha,delta (degrees) of variable  stars for heliocentric correction"
  write(*,*) "ele= epoch, period of variable stars for phase output"
  write(*,*) "geo= geographic coordinates (degrees, +east,+north) of site (for -oi)"
  write(*,*) "suf= suffix of listing files (default = 'MAT')"
  write(*,*) "tol= upper limit for coordinate search (default=10)"
  write(*,*) "line_var  line_comp1...= line with stars"
  write(*,*) "xcoo1,ycoo1 xcoo2,ycoo2 ...= x,y coordinates of stars" 
  write(*,*) "image1...= name(s) of simple image(s)"
  write(*,*)
endif

end

! ------------------------------------------------------------------
! dummy procedure
!
!  subroutine munilist(namfits,column,nstars,stars,outlst, &
!                      min0,per,alpha,delta,sufix,status)
!
!  implicit none
!
!  character(len=*) :: namfits,sufix
!  logical :: outlst
!  integer :: column,nstars,stars(nstars),status
!  real(selected_real_kind(16)) :: min0,per,alpha,delta
!
!  write(*,*) trim(namfits),' ',trim(sufix)
!  write(*,*) outlst
!  write(*,*) column,nstars
!  write(*,*) stars(nstars)
!  write(*,*) min0,per
!  write(*,*) alpha,delta
!  
!  status = 0
!
!  end

